<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home/actor-list' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>演员管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增演员</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加演员表单 -->
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="演员信息">
        <el-input v-model="form.actorName" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="演员头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:9000/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <el-image
            v-if="form.actorAvatar"
            :src="form.actorAvatar"
            fit="fit"
            class="avatar"
          ></el-image>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即添加</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        //用于绑定表单数据
        actorName: "",
        actorAvatar: "",
      },
    };
  },
  methods: {
    /**
     * 提交表单
     */
    onSubmit() {
      //发送http post请求 提交添加演员的参数  实现添加业务
      console.log("submit!");
      //封装了
      this.$http.ActorApi.add(this.form).then((res) => {
        console.log("添加演员成功", res);
        if (res.data.code == 200) {
          this.$message({
            type: "success",
            message: "添加成功！",
          });
        }
        this.$router.push("/home/actor-list");
      });
      //这个没封装的写法
      // this.axios
      //   .post(
      //     "/movie-actor/add",
      //     `actorName=${this.form.actorName}&actorAvatar=${this.form.actorAvatar}`
      //   )
      //   .then((res) => {
      //     console.log("添加演员成功", res);
      //     if (res.data.code == 200) {
      //       this.$message({
      //         type: "success",
      //         message: "添加成功！",
      //       });
      //     }
      //     this.$router.push("/home/actor-list");
      //   });
    },
    /**
     * 处理上传成功后的业务，回显图片
     */
    handleAvatarSuccess(res, file) {
      console.log(res);
      console.log(file);
      if (res.code === 200) {
        this.form.actorAvatar = res.data; //将返回来的图片赴京 赋值给actorAvatar
      }
    },
    /**
     * 上传图片之前执行的业务  判断图片的格式
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
